package org.stvd.common.security.filter.web;

import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.stvd.common.utils.CharsetKit;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class CharacterTranscodeRequest extends HttpServletRequestWrapper implements ServletRequest {

    private final Map params;
    
    private final String encoding;

    public CharacterTranscodeRequest(HttpServletRequest request, Map newParams) {
        super(request);
        this.params = newParams;
        this.encoding = request.getCharacterEncoding();
    }

    public Map getParameterMap() {
        return params;
    }
    
    public String getRequestEncoding() {
        return encoding;
    }

    public Enumeration getParameterNames() {
        Vector l = new Vector(params.keySet());
        return l.elements();
    }

    private String transcode(String origin) {
        String rtValue = "";
        
        if (!CharsetKit.UTF_8.equalsIgnoreCase(getRequestEncoding())) {
            rtValue = CharsetKit.convert(origin, CharsetKit.ISO_8859_1, this.getCharacterEncoding());
        } else {
            rtValue = origin;
        }
        
        if (rtValue.indexOf('&') > -1)
            rtValue = rtValue.replaceAll("&", "&amp;");
        if (rtValue.indexOf('\"') > -1)
            rtValue = rtValue.replaceAll("\"", "&quot;");
//        if (rtValue.indexOf(' ') > -1)
//            rtValue = rtValue.replaceAll(" ", "&nbsp;");
        if (rtValue.indexOf('<') > -1)
            rtValue = rtValue.replaceAll("<", "&lt;");
        if (rtValue.indexOf('>') > -1)
            rtValue = rtValue.replaceAll(">", "&gt;");
        if (rtValue.indexOf('\'') > -1)
            rtValue = rtValue.replaceAll("'", "&#39;");
        
        return rtValue;
    }

    public String[] getParameterValues(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] value = (String[]) v;
            String[] rtValue = value.clone();
            for (int i = 0; i < value.length; i++) {
                rtValue[i] = transcode(rtValue[i]);
            }
            return (String[]) rtValue;
        } else if (v instanceof String) {
            String value = (String) v;
            return new String[] { (String) transcode(value) };
        } else {
            return new String[] { v.toString() };
        }
    }

    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return transcode(strArr[0]);
            } else {
                return null;
            }
        } else if (v instanceof String) {
            return transcode((String) v);
        } else {
            return v.toString();
        }
    }
}
